package com.awspaas.user.apps.shhtaerospaceindustrial.apartmanage;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge.weComSendAnnouncementUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class RentingoutApplySendMessage extends ExecuteListener{

	public String getDescription() {
		return "办理退租审批结束，给退租人发短信消息及删除入住信息。";
	}
	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		// TODO Auto-generated method stub
				boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "同意");
				if(flag_ok) {
					//获取流程实例ID
					String processInstId = param.getProcessInstance().getId();
					//根据流程实例ID获取任务表内数据
					List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_SH_RENTINGOUT  WHERE BINDID = ? ", 
							new ColumnMapRowMapper(), new Object[] {processInstId});
					Map<String , Object>  jcbd = headlist.get(0);
					String FID=CoreUtil.objToStr(jcbd.get("FID"));//租户表id
					String APPLYNAMEID=CoreUtil.objToStr(jcbd.get("APPLYNAMEID"));//租户账号
					String APPLYUNIT=CoreUtil.objToStr(jcbd.get("APPLYUNIT"));//申请单位
					String APPLYNAME=CoreUtil.objToStr(jcbd.get("APPLYNAME"));//申请人
					String APPLYTELEPHONE=CoreUtil.objToStr(jcbd.get("APPLYTELEPHONE"));//申请人电话
					String ID=CoreUtil.objToStr(jcbd.get("ID"));//ID
					String ACCOUNTID=CoreUtil.objToStr(jcbd.get("ACCOUNTID"));//住宅号id
					String sqlbindid = "select bindid from BO_EU_SH_RCGY_ROOM_PSN t where  t.ID = '"
							+ FID + "'";
					String bodybindid = CoreUtil.objToStr(DBSql.getString(sqlbindid, "bindid"));// bindid
					//删除租户信息
					String querySql ="select ID from  BO_EU_SH_RCGY_ROOM_PSN    where YUANGONGZH = '"+ APPLYNAMEID+ "'";
					List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
					if (urlList != null && urlList.size() > 0) {
						for (int i = 0; i < urlList.size(); i++) {
							Map<String , Object> dataprobleminvent = urlList.get(i);
							String rzdaID=CoreUtil.objToStr(dataprobleminvent.get("ID"));//租户表id
							SDK.getBOAPI().remove("BO_EU_SH_RCGY_ROOM_PSN", rzdaID);
						}
					}
					//更新入住人数字段
					String queryUrl ="SELECT ID FROM BO_EU_SH_RCGY_ROOM_PSN  where bindid ='"+bodybindid+"'";
					List<Map<String, Object>> queryUrlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
					if(queryUrlList ==null ||queryUrlList.isEmpty()) {
						String updatenosql = "update  BO_EU_SH_RCGY_ROOM set INROOMNUM = '0' where id ='" + ACCOUNTID + "'";
						DBSql.update(updatenosql);
					}else {
						String updatenosql = "update  BO_EU_SH_RCGY_ROOM set INROOMNUM = '"+queryUrlList.size()+"' where id ='" + ACCOUNTID + "'";
						DBSql.update(updatenosql);
					}
					//发送企业微信消息
					if(!"".equals(APPLYUNIT) && !"".equals(APPLYNAMEID) ) {
						String sqluserId ="select EXT5  from ORGUSER  where USERID='"+APPLYNAMEID+"'";
				        String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
				        String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+APPLYUNIT+"'";
				        List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
				        if (jbData.size() > 0 && !"".equals(weixinid)) {
				        	String content="【人才公寓管理】您好！"+APPLYUNIT+APPLYNAME+"已为您办理完毕退租流程，请及时到人才公寓物业办理相关退租手续。";
				        	String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
							String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
							String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
							String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
							weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",content,0,agentid,acessToken);
				        }
					}
				}
	}

}
